uniform sampler2D theTexture;
uniform sampler2D normalMap;

// computed in vertex shader
varying vec3 N, L;

void main() {
	// get the texture colour
	vec3 	textureColour = vec3(texture2D(theTexture, gl_TexCoord[0].st));
	
	// get the normal map value
	vec3	normalMapValue = normalize(vec3(texture2D(normalMap, gl_TexCoord[0].st)));
	
	// readjust to unit space
	normalMapValue.x -= 0.5;
	normalMapValue.y -= 0.5;
	normalMapValue.z -= 0.5;
	normalMapValue *= 2.0;
	
	// calculate diffuse intensity
	float 	diffuseIntensity = max(0.0, dot(normalize(N), normalize(L)));
	
	// calculate map intensity
	float	mapI = max(0.0, dot(normalMapValue, vec3(0.0, 0.0, 1.0)));
	
	// assign texture colour
	gl_FragColor.rgb = gl_Color.rgb * textureColour.rgb;
	
	// factor in diffuseIntensity and map intensity
	gl_FragColor.rgb *= (diffuseIntensity * mapI);
	
}
